package org.amse.korovkin.ciphering.view;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import org.amse.korovkin.ciphering.model.Model;
import org.amse.korovkin.ciphering.model.cipher.ICipher;
import org.amse.korovkin.ciphering.model.cipher.monocipher.MonoCipher;
import org.amse.korovkin.ciphering.model.cipher.splinewavelet.Spline1WaveletCipher;
import org.amse.korovkin.ciphering.model.cipher.splinewavelet.Spline3WaveletCipher;
import org.amse.korovkin.ciphering.view.MyJDialogActions.*;

public abstract class MyJDialog extends JDialog{
    public static final int CLOSE = -1;
    public static final int KEY = 1;
    public static final int STATISTIC = 2;

    protected int returnValue = CLOSE;
    protected CipheringJFrame parent;

    protected JLabel jl = new JLabel();
    protected JTextField tf = new JTextField();
    protected JButton action = new JButton("Action");

    private JRadioButton button1;
    private JRadioButton button2;
    private JRadioButton button3;

    private JButton open = new JButton("...");
    private JButton ok = new JButton("OK");
    private JButton cancel = new JButton("Cancel");

    private final int height = 200;
    private final int width = 400;
    
    public MyJDialog(CipheringJFrame jFrame, String title) {
        super(jFrame, title, true);
        parent = jFrame;
        setSize(width, height);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocation(parent.getX() + (parent.getWidth() - width) / 2
                , parent.getY() + (parent.getHeight() - height) / 2);
        setResizable(false);
        initComponents();
    }

    public void pressOk() {
    }

    public void pressCancel() {
        returnValue = CLOSE;
        this.dispose();
    }

    public void setFilePath(String path) {
        tf.setText(path);
        tf.postActionEvent();
    }

    public int getCloseOparetion() {
        return returnValue;
    }
    
    public void setText(String text) {
        jl.setText(text);
    }

    public String getMode() {
        if (button1.isSelected()) {
            return button1.getName();
        } else if (button2.isSelected()) {
            return button2.getName();
        } else return button3.getName();
    }
    private void initComponents() {
        Container c = getContentPane();
        c.setLayout(new FlowLayout());

        jl.setHorizontalAlignment(SwingConstants.RIGHT);
        tf.setPreferredSize(new Dimension(200, 20));

        Box topBox = Box.createHorizontalBox();
        topBox.add(jl);
        topBox.add(Box.createHorizontalStrut(5));
        topBox.add(tf);
        topBox.add(Box.createHorizontalStrut(5));
        topBox.add(open);
        topBox.add(Box.createHorizontalStrut(5));
        topBox.add(action);

        Box bottomBox = Box.createHorizontalBox();
        bottomBox.add(ok);
        bottomBox.add(Box.createHorizontalStrut(10));
        bottomBox.add(cancel);

        Box vBox = Box.createVerticalBox();
        vBox.add(createCiphersBox());
        vBox.add(topBox);
        vBox.add(Box.createVerticalStrut(5));
        vBox.add(bottomBox);

        this.add(vBox);
        open.setAction(new OpenAction(this));
        open.setText(null);
        ok.setAction(new OkAction(this));
        cancel.setAction(new CancelAction(this));

        Dimension buttonDimension = cancel.getPreferredSize();
        ok.setMinimumSize(buttonDimension);
        ok.setMaximumSize(buttonDimension);
        ok.setPreferredSize(buttonDimension);
    }

    private Box createCiphersBox(){
        button1 = new JRadioButton(Model.cipher1);
        button2 = new JRadioButton(Model.cipher2);
        button3 = new JRadioButton(Model.cipher3);

        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Model.getInstance().setCipher(Model.cipher1);
            }
        });
        button2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Model.getInstance().setCipher(Model.cipher2);
            }
        });
        button3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Model.getInstance().setCipher(Model.cipher3);
            }
        });

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(button1);
        buttonGroup.add(button2);
        buttonGroup.add(button3);
        button1.setSelected(true);
        Model.getInstance().setCipher(Model.cipher1);

        Box resultBox = Box.createVerticalBox();

        Box box1 = Box.createHorizontalBox();
        box1.add(button1);

        Box box2 = Box.createHorizontalBox();
        box2.add(button2);

        Box box3 = Box.createHorizontalBox();
        box3.add(button3);

        resultBox.add(box1);
        resultBox.add(Box.createVerticalStrut(2));
        resultBox.add(box2);
        resultBox.add(Box.createVerticalStrut(2));
        resultBox.add(box3);

        return resultBox;
    }
}

